package com.lcn.tm.service.transaction;

import com.lcn.comm.domain.TxGroup;
import com.lcn.comm.domain.TxGroupManager;
import com.lcn.comm.domain.TxRequest;
import com.lcn.comm.service.redis.RedisService;
import com.lcn.comm.service.transaction.TransactionConfirmService;
import com.lcn.comm.service.transaction.TransactionGroupService;
import com.lcn.comm.util.RequestIdGenerator;
import com.weibo.api.motan.config.springsupport.annotation.MotanService;
import com.weibo.api.motan.util.LoggerUtil;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;

/**
 * <p>类说明</p>
 *
 * @author 张峰 zfvip_it@163.com
 * @create: 下午8:26
 */
@MotanService
public class TransactionGroupServiceImpl implements TransactionGroupService {

    @Resource
    private RedisService redisService;

    @Resource
    private TransactionConfirmService transactionConfirmService;

    /**
     * 创建事务组
     *
     * @param groupId
     * @return
     */
    public TxGroup createTransactionGroup(String groupId) {
        TxGroup txGroup = new TxGroup();
        if (StringUtils.isNotEmpty(groupId)) {
            txGroup.setIsCommit(1);
        } else {
            groupId = RequestIdGenerator.getRequestId();
        }
        txGroup.setGroupId(groupId);
        //开始时间
        txGroup.setStartTime(System.currentTimeMillis());
        //异步将事务组缓存到本地
        redisService.setValue(groupId, txGroup);
        //加入本地缓存
        TxGroupManager.getInstance().addGroup(txGroup);
        return txGroup;
    }

    /**
     * 添加事务组
     *
     * @param isHasGroup
     * @param txRequest
     * @return
     */
    public boolean addTransactionGroup(boolean isHasGroup, TxRequest txRequest) {
        TxGroup txGroup = TxGroupManager.getInstance().getGroup(txRequest.getGroupId());
        if (txGroup != null) {
            addGroup(txGroup, txRequest);
            return true;
        } else {
            //从redis中获取
            txGroup = redisService.getTxGroupByKey(txRequest.getGroupId());
            if (txGroup != null) {
                addGroup(txGroup, txRequest);
                return true;
            } else {
                LoggerUtil.info("添加事务组失败,groupId[{}]", txRequest.getGroupId());
                return false;
            }
        }
    }

    /**
     * 添加事务组
     *
     * @param txGroup
     * @param txRequest
     */
    private void addGroup(TxGroup txGroup, final TxRequest txRequest) {
        txGroup.addRequest(txRequest);
        TxGroupManager.getInstance().addGroup(txGroup);
        redisService.setValue(txGroup.getGroupId(), txGroup);
    }

    /**
     * 关闭事务组
     *
     * @param state 1:关闭成功 0:失败
     * @return
     */
    public int closeTransactionGroup(String groupId, int state) {
        //获取事务组
        TxGroup txGroup = TxGroupManager.getInstance().getGroup(groupId);
        if (txGroup != null) {
            return closeGroup(txGroup, state);
        } else {
            txGroup = redisService.getTxGroupByKey(groupId);
            if (txGroup != null) {
                return closeGroup(txGroup, state);
            }
        }
        return 0;
    }

    /**
     * 关闭事务组
     *
     * @param txGroup
     * @param state
     * @return
     */
    private int closeGroup(TxGroup txGroup, int state) {
        //更新事务组状态
        txGroup.setState(state);
        //关闭事务组
        txGroup.setHasOver(true);
        //更新本地缓存
        TxGroupManager.getInstance().addGroup(txGroup);
        //关闭事务组之前先缓存事务组信息
        redisService.setValue(txGroup.getGroupId(), txGroup);
        //提交事务处理
        return transactionConfirmService.confirm(txGroup);
    }
}
